/**
 * 
 */
package soa.security.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @author Cre.Gu
 *
 */
public class BeanUtils {

	public static void blank2Null(Object source) throws BeansException {
		Assert.notNull(source, "Source must not be null");

		PropertyDescriptor[] sourcePds = org.springframework.beans.BeanUtils
				.getPropertyDescriptors(source.getClass());

		for (PropertyDescriptor sourcePd : sourcePds) {
			Method writeMethod = sourcePd.getWriteMethod();

			if (writeMethod != null) {

				Method readMethod = sourcePd.getReadMethod();
				if (readMethod != null
						&& ClassUtils.isAssignable(
								writeMethod.getParameterTypes()[0],
								readMethod.getReturnType())) {
					try {
						if (!Modifier.isPublic(readMethod.getDeclaringClass()
								.getModifiers())) {
							readMethod.setAccessible(true);
						}
						Object value = readMethod.invoke(source);
						if (!Modifier.isPublic(writeMethod.getDeclaringClass()
								.getModifiers())) {
							writeMethod.setAccessible(true);
						}

						if (value instanceof String) {
							if (StringUtils.isBlank((String) value))
								value = null;
							else
								continue;
						}
						writeMethod.invoke(source, value);
					} catch (Throwable ex) {
						throw new FatalBeanException(
								"Could not copy property '"
										+ sourcePd.getName()
										+ "' from source to target", ex);
					}
				}
			}
		}
	}

	public static void copyPropertiesWithoutNull(Object source, Object target)
			throws BeansException {

		Assert.notNull(source, "Source must not be null");
		Assert.notNull(target, "Target must not be null");

		Class<?> actualEditable = target.getClass();

		PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils
				.getPropertyDescriptors(actualEditable);

		for (PropertyDescriptor targetPd : targetPds) {
			Method writeMethod = targetPd.getWriteMethod();
			if (writeMethod != null) {
				PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils
						.getPropertyDescriptor(source.getClass(),
								targetPd.getName());
				if (sourcePd != null) {
					Method readMethod = sourcePd.getReadMethod();
					if (readMethod != null
							&& ClassUtils.isAssignable(
									writeMethod.getParameterTypes()[0],
									readMethod.getReturnType())) {
						try {
							if (!Modifier.isPublic(readMethod
									.getDeclaringClass().getModifiers())) {
								readMethod.setAccessible(true);
							}
							Object value = readMethod.invoke(source);
							if (!Modifier.isPublic(writeMethod
									.getDeclaringClass().getModifiers())) {
								writeMethod.setAccessible(true);
							}

							if (value == null)
								continue;

							writeMethod.invoke(target, value);
						} catch (Throwable ex) {
							throw new FatalBeanException(
									"Could not copy property '"
											+ targetPd.getName()
											+ "' from source to target", ex);
						}
					}
				}
			}
		}
	}
}
